package com.credit.jumu.playcard.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.credit.jumu.playcard.R;
import com.credit.jumu.playcard.adapter.ChooseListAdapter;
import com.credit.jumu.playcard.adapter.HomeListAdapter;
import com.credit.jumu.playcard.bean.HomeInfo;
import com.credit.jumu.playcard.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/10/20/020.
 */

public class ChooseCardDialog extends Dialog {
	private final Dialog dialog;
	private Context context;
	private ListView dialoglist;
	private List<HomeInfo> mlistInfo = new ArrayList<HomeInfo>();
	private OnItemClickListener onItemClickListener;
	private ImageView cloer;

	public interface OnItemClickListener<T> {
		public void onItemClick(int position);
	}

	public ChooseCardDialog(@NonNull Activity context, List<HomeInfo> mlistInfo, final OnItemClickListener onItemClickListener) {
		super(context);
		this.context = context;
		this.onItemClickListener = onItemClickListener;
		dialog = new Dialog(context, R.style.ListDialogStyle);
		setOwnerActivity((Activity) context);
		dialog.setContentView(R.layout.dialog_list);
		dialoglist = dialog.findViewById(R.id.dialoglist);
		cloer = dialog.findViewById(R.id.cloer);
		this.mlistInfo = mlistInfo;
		cloer.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				cancelDialog();
			}
		});
		dialoglist.setAdapter(new ChooseListAdapter(mlistInfo, context));
		dialoglist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if (onItemClickListener != null) {
					onItemClickListener.onItemClick(position);
				}
			}
		});
		Window dialogWindow = dialog.getWindow();
		dialogWindow.setGravity(Gravity.BOTTOM);
		WindowManager.LayoutParams layoutpara = dialogWindow
				.getAttributes();
		dialogWindow.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
		layoutpara.width = context.getWindow().getAttributes().width;
		dialogWindow.setAttributes(layoutpara);
	}

	public void showDialog() {
		if (null != dialog) {
			dialog.show();
		}
	}

	public void cancelDialog() {
		if (null != dialog || dialog.isShowing()) {
			dialog.cancel();
		}
	}
}
